<?php


namespace App\Controller\Api\V1;

use App\Common\BeseController;
use App\Model\Mysql\ConfigModel;
use App\Model\Mysql\OrderModel;
use App\Model\Mysql\UserExamineModel;
use App\Model\Mysql\UserIntegralLogModel;
use App\Model\Mysql\UserInviteModel;
use App\Model\Mysql\UserLevelModel;
use App\Model\Mysql\UserModel;


class User extends BeseController
{


    public function index()
    {
        $input = $this->input;

        $userId = $input['user']['id'] ?? 1;

        $user = UserModel::getOne(['id' => $userId], ['nickname', 'headimg', 'integral', 'invite_img', 'level_id']);

        $user['level_name'] = UserLevelModel::getValue(['id' => $user['level_id']], 'name');

        $user->hidden(['level_id']);

        $content = ConfigModel::getValue(['id' => 1], 'content');

        return $this->responseSuccess('请求成功', ['user' => $user, 'content' => $content]);
    }

    public function examine()
    {
        $input = $this->input;

        $userId = $input['user']['id'] ?? 1;

        $page = empty($input['page']) ? 0 : $input['page'] - 1;
        $size = empty($input['size']) ? 10 : $input['size'];

        $where = ['user_id' => $userId];
        if (!empty($input['status'])) {
            $where['status'] = $input['status'];
        }
        $data = UserExamineModel::getList($where, ['id', 'project_id', 'status'], [$page * $size, $size]);

        foreach ($data as &$value) {
            $project = $value->project(['id', 'thumb', 'name', 'integral', '`describe`', 'new_status', 'type']);
            $value['thumb'] = $project['thumb'];
            $value['name'] = $project['name'];
            $value['integral'] = $project['integral'];
            $value['describe'] = $project['describe'];
            $value['new_status'] = $project['new_status'];
            $value['type'] = $project['type'];

            $value->hidden(['project']);
        }
        return $this->responseSuccess('请求成功', $data);
    }

    public function invite()
    {
        $input = $this->input;

        $userId = $input['user']['id'] ?? 1;

        $user = UserInviteModel::getList(['pid_id' => $userId], ['pid_id', 'user_id']);

        foreach ($user as &$value) {
            $info = $value->user(['id', 'nickname', 'integral', 'create_time', 'level_id']);
            $value['nickname'] = $info['nickname'];
            $value['integral'] = $info['integral'];
            $value['create_time'] = date('Y-m-d', $info['create_time']);
            $value['level_name'] = UserLevelModel::getValue(['id' => $info['level_id']], 'name');
            $value->hidden(['user', 'pid_id', 'user_id', 'level_id']);
        }

        return $this->responseSuccess('请求成功', $user);
    }

    public function integralLog()
    {
        $input = $this->input;

        $userId = $input['user']['id'] ?? 1;

        $user = UserIntegralLogModel::getList(['user_id' => $userId, 'status' => 1], ['order_id', 'integral', 'create_time']);

        foreach ($user as &$value) {
            $value['create_time'] = date('Y-m-d', $value['create_time']);
            $value['name'] = OrderModel::getValue(['id' => $value['order_id']], 'client_name');
        }
        return $this->responseSuccess('请求成功', $user);
    }

}